package com.allbluefish.learn.util;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.JDomSerializer;
import org.htmlcleaner.TagNode;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import com.google.common.collect.Lists;

public class WebClipUtils {

	public static Document getDocumentByURL(String url, String charset) throws MalformedURLException, IOException {
		HtmlCleaner htmlCleaner = new HtmlCleaner();
		CleanerProperties props = htmlCleaner.getProperties();
		TagNode node = htmlCleaner.clean(new URL(url), charset);
		JDomSerializer jdomSerializer = new JDomSerializer(props, true);
		Document doc = jdomSerializer.createJDom(node);
		return doc;
	}

	public static List<Element> getElementsByTagName(Document doc, String tagName) {
		List<Element> eleList = Lists.newArrayList();
		buildList(doc.getRootElement(), tagName, eleList);
		return eleList;
	}

	private static void buildList(Element rootEle, String tagName, List<Element> eleList) {
		if (rootEle.getName().equals(tagName)) {
			eleList.add(rootEle);
		}
		@SuppressWarnings("unchecked")
		List<Element> list = rootEle.getChildren();
		for (Iterator<Element> iter = list.iterator(); iter.hasNext();) {
			Element ele = (Element) iter.next();
			buildList(ele, tagName, eleList);
		}
	}

	public static void printElement(Element ele) throws IOException {
		XMLOutputter outputer = new XMLOutputter();
		Format format = outputer.getFormat();
		format.setEncoding("utf-8");
		outputer.setFormat(format);
		outputer.output(ele, System.out);
	}

	public static void main(String[] args) throws Exception {

		Document doc = getDocumentByURL("http://www.huanqiu.com", "utf-8");
		//Element rootEle = doc.getRootElement();
		//System.out.println(XPathHelper.getPathString(rootEle));

		final String tagName = "div";
		List<Element> list = getElementsByTagName(doc, tagName);
		System.out.println(list.size());

		Iterator<Element> iter = list.iterator();
		while (iter.hasNext()) {
			Element ele = (Element) iter.next();
			//System.out.println();
			//System.out.println("*****************************************");
			//System.out.println(XPathHelper.getPathString(ele));
			//System.out.println("*****************************************");
			printElement(ele);
		}

	}
}
